﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.DTOs;
using Hartley.ApplicationLogic;
using Hartley.Util;
namespace WebApplication1
{
    public partial class viewTeamsByYr : System.Web.UI.Page
    {
        protected int Rows
        {
            get
            {
                return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
            }
            set
            {
                ViewState["Rows"] = value;
            }
        }

        // Columns property to hold the Columns in the ViewState
        protected int Columns
        {
            get
            {
                return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
            }
            set
            {
                ViewState["Columns"] = value;
            }
        }

        public List<TeamDTO> AllTeams
        {
            get
            {
                if (Session["AllTeams"] == null)
                {
                    return sportsAL.GetAllTeams();
                }
                else
                {
                    return (List<TeamDTO>)Session["AllTeams"];
                }
            }
            set
            {
                Session["AllTeams"] = value;
            }
        }

        SportsAL sportsAL;
        SportsUtil sportsUtil;
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 5;
        private const int YEAR_PER_ROW = 4;
        List<int> allSportsYrs;
        string yrWiseQueryString;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindData();
            }

        }

        private void CreateDynamicTable(List<int> allSportsYrsOfOnePage)
        {
            PlaceHolder1.Controls.Clear();
            sportsUtil = new SportsUtil();
            sportsAL = new SportsAL();
            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allSportsYrsOfOnePage.Count / YEAR_PER_ROW;
            int tblCols = 0;


            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder1.Controls.Add(tbl);
            // Now iterate through the table and add your controls 
            for (int i = 0; i <= tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");



                if (i != tblRows && tblRows > 1)
                {
                    tblCols = YEAR_PER_ROW;
                }
                else
                {
                    tblCols = allSportsYrsOfOnePage.Count % YEAR_PER_ROW;
                }


                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();

                    HyperLink hlAthleticYrCell = new HyperLink();
                    hlAthleticYrCell.Text = allSportsYrsOfOnePage[i * YEAR_PER_ROW + j].ToString();

                    if (string.IsNullOrEmpty(yrWiseQueryString))
                    {
                        hlAthleticYrCell.NavigateUrl = "viewSportsDataAsGrid.aspx?" + sportsUtil.encryptQueryStrings( allSportsYrsOfOnePage[i * YEAR_PER_ROW + j].ToString());
                    }
                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hlAthleticYrCell);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }


        private void BindData()
        {

            sportsAL = new SportsAL();
            allSportsYrs = new List<int>();
            List<int> allSportsYrsOfOnePage = new List<int>();

            yrWiseQueryString = Request.QueryString.ToString();

            if (string.IsNullOrEmpty(yrWiseQueryString))
            {
                allSportsYrs = sportsAL.GetAllTeams().OrderByDescending(b => b.Year).Select(a => a.Year).Distinct().ToList();

            }






            // get the total rows 
            double totalRows = (int)allSportsYrs.Count;

            lblAllSportsYrPage.Text = "Displaying all Sports Data of College - Page ";
            lblAllSportsYrOf.Text = "  of  ";
            lblAllSportsYrTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllSportsYrCurrPage.Text = currentPageNumber.ToString();

            if (Convert.ToInt32(lblAllSportsYrTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblAllSportsYrTotalPage.Text) <= 0)
                {
                    btnAllSportsYrFirst.Enabled = false;
                    btnAllSportsYrPrev.Enabled = false;
                    btnAllSportsYrNext.Enabled = false;
                    btnAllSportsYrLast.Enabled = false;


                }

                if (currentPageNumber == 1)
                {
                    btnAllSportsYrFirst.Enabled = false;
                    btnAllSportsYrPrev.Enabled = false;

                    if (Int32.Parse(lblAllSportsYrTotalPage.Text) == 1)
                    {
                        allSportsYrsOfOnePage = allSportsYrs.GetRange(0, Convert.ToInt32(totalRows));

                        btnAllSportsYrNext.Enabled = false;
                        btnAllSportsYrLast.Enabled = false;


                    }
                    else if (Int32.Parse(lblAllSportsYrTotalPage.Text) == 2)
                    {
                        allSportsYrsOfOnePage = allSportsYrs.GetRange(0, PAGE_SIZE * YEAR_PER_ROW);

                        btnAllSportsYrNext.Enabled = true;
                        btnAllSportsYrLast.Enabled = true;




                    }
                }


                else
                {


                    if (currentPageNumber == Int32.Parse(lblAllSportsYrTotalPage.Text))
                    {
                        allSportsYrsOfOnePage = allSportsYrs.GetRange((currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW));

                        btnAllSportsYrNext.Enabled = false;
                        btnAllSportsYrLast.Enabled = false;

                    }
                    else
                    {
                        allSportsYrsOfOnePage = allSportsYrs.GetRange((currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW, PAGE_SIZE * YEAR_PER_ROW);

                        btnAllSportsYrNext.Enabled = true;
                        btnAllSportsYrLast.Enabled = true;


                    }
                }




            }
            else
            {
                if (currentPageNumber == 1)
                {
                    allSportsYrsOfOnePage = allSportsYrs.GetRange(0, PAGE_SIZE * YEAR_PER_ROW);

                    btnAllSportsYrFirst.Enabled = false;
                    btnAllSportsYrPrev.Enabled = false;
                    btnAllSportsYrNext.Enabled = true;
                    btnAllSportsYrLast.Enabled = true;


                }



                else
                {
                    btnAllSportsYrPrev.Enabled = true;
                    btnAllSportsYrFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblAllSportsYrTotalPage.Text))
                    {
                        allSportsYrsOfOnePage = allSportsYrs.GetRange((currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW));

                        btnAllSportsYrNext.Enabled = false;
                        btnAllSportsYrLast.Enabled = false;

                    }
                    else
                    {
                        allSportsYrsOfOnePage = allSportsYrs.GetRange((currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW, PAGE_SIZE * YEAR_PER_ROW);

                        btnAllSportsYrNext.Enabled = true;
                        btnAllSportsYrLast.Enabled = true;

                    }


                }
            }

            // gvAllMeeting.DataSource = allMeetingsOfOnePage;
            // gvAllMeeting.DataBind();
            CreateDynamicTable(allSportsYrsOfOnePage);


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / (PAGE_SIZE * YEAR_PER_ROW));

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllSportsYrCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllSportsYrCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllSportsYrTotalPage.Text);
                    break;
            }


            BindData();
        }

    }
}